from optparse import OptionParser
import os

def create_metadata_skel():
    from browsertests.tests.models import Test
    ids = set(test.pk for test in Test.objects.all())
    #print "Tests count", len(ids)
    #dirs = set()
    dir_to_count = {}
    for id in ids:
        #print "dirname:", os.path.dirname(id)
        dir = os.path.dirname(id)
        dir_to_count[dir] = dir_to_count.setdefault(dir, 0) + 1
        #dirs.add()
    for dir in sorted(dir_to_count.iterkeys()):
        count = dir_to_count[dir]
        print
        print "# %s tests" % count
        print "#^%s/.*$" % dir
        print "#  specs:"

if __name__ == "__main__":
    usage = "%prog [options]"
    parser = OptionParser(usage=usage)
    parser.add_option('--settings', help='Python path to settings module, '
                      'e.g. "myproject.settings". If this isn\'t provided, '
                      'the DJANGO_SETTINGS_MODULE environment variable will'
                      ' be used.')
    options, args = parser.parse_args()
    if options.settings:
        os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
    elif "DJANGO_SETTINGS_MODULE" not in os.environ:
        parser.error("DJANGO_SETTINGS_MODULE is not set in the environment. "
                      "Set it or use --settings.")

    create_metadata_skel()
